package com.pzhu.user.controller.manage;

import com.pzhu.base.common.utils.result.PageData;
import com.pzhu.base.common.utils.result.Result;
import com.pzhu.base.common.utils.result.Results;
import com.pzhu.user.dao.UserDAO;
import com.pzhu.user.domain.entity.User;
import com.pzhu.user.domain.ro.UserCreateRO;
import com.pzhu.user.domain.ro.UserSearchRO;
import com.pzhu.user.domain.ro.UserUpdateRO;
import com.pzhu.user.domain.vo.UserSearchVO;
import com.pzhu.user.rpc.client.AuthClientService;
import com.pzhu.user.service.impl.UserService;
import jakarta.validation.Valid;
import java.util.Arrays;
import java.util.Collection;
import java.util.List;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

@RequestMapping("/api/management/users")
@RestController
@RequiredArgsConstructor
@Validated
public class UserManageController {

    private final UserService userService;

    /**
     * 用户列表
     *
     * @param userSearchRo 用户搜索请求
     * @return {@link Result }<{@link Collection }<{@link UserSearchVO }>>
     */
    @GetMapping
    public Result<Collection<UserSearchVO>> list(UserSearchRO userSearchRo) {
        PageData<UserSearchVO> search = userService.search(userSearchRo);
        return Results.page(search);
    }

    /**
     * 创建用户
     *
     * @param userCreateRo 用户创建请求
     * @return {@link Result }<{@link Long }>
     */
    @PostMapping
    public Result<Long> create(@Valid @RequestBody UserCreateRO userCreateRo) {
        Long userId = userService.createUser(userCreateRo);
        return Results.success(userId);
    }

    /**
     * 更新用户信息
     *
     * @param userId 用户id
     * @param userUpdateRo 用户更新请求
     * @return {@link Result }<{@link ? }>
     */
    @PutMapping("/{userId}")
    public Result<Boolean> update(@PathVariable Long userId, @Valid @RequestBody UserUpdateRO userUpdateRo) {
        boolean result = userService.updateUser(userId, userUpdateRo);
        return Results.success(result);
    }

    /**
     * 用户id删除
     *
     * @param userId 用户id
     * @return {@link Result }<{@link Boolean }>
     */
    @DeleteMapping("/{userId}")
    public Result<Boolean> delete(@PathVariable Long userId) {
        boolean result = userService.deleteUserById(userId);
        return Results.success(result);
    }

    /**
     * 批量删除
     *
     * @param userIdList 用户id清单
     * @return {@link Result }<{@link Boolean }>
     */
    @DeleteMapping
    public Result<Boolean> batchDelete(String userIdList) {
        List<Long> userIds =
                Arrays.stream(userIdList.split(",")).map(Long::valueOf).toList();
        boolean result = userService.batchDeleteUserByIds(userIds);
        return Results.success(result);
    }

    /**
     * 重置密码
     *
     * @param userId 用户id
     * @return {@link Result }<{@link Boolean }>
     */
    @PostMapping("/{userId}/resetPassword")
    public Result<Boolean> resetPassword(@PathVariable Long userId) {
        boolean result = userService.restPassword(userId);
        return Results.success(result);
    }

    private final AuthClientService authClientService;
    private final UserDAO userDAO;

    @PostMapping("set")
    public void initUserPassword(){
        String generated = authClientService.generatePasswords("123456");
        List<User> users = userDAO.selectAll();
        users.forEach(u -> u.setPassword(generated));
        userDAO.updateBatch(users);
    }
}
